import React from 'react';
import { inject, observer } from 'mobx-react';
import { message, Modal, Button } from 'antd';
import { WeaTableNew } from "comsMobx";
import { WeaAuth, WeaPopoverHrm, WeaLocaleProvider, WeaRightMenu, WeaSearchGroup, WeaTab } from "ecCom";
import * as Util from '../../../util/Util.js';
const toJS = mobx.toJS;
const confirm = Modal.confirm
const getLabel = WeaLocaleProvider.getLabel;
const WeaTable = WeaTableNew.WeaTable;
@inject("goalDetailStore")
@observer
export default class GoalShare extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            showOperation: false, //显示操作框
            selectedRowKeys: [], //选中行
            rightMenu: [
                {
                    key: "0",
                    icon: <i className="icon-coms-Add-to" />,
                    content: getLabel(365, '新建'),
                    onClick: () => this.addCreate()
                },
                {
                    key: "1",
                    icon: <i className="icon-coms-Delete" />,
                    disabled: true,
                    content: getLabel(32136, '批量删除'),
                    onClick: () => this.del()
                }
            ],
            _height: Util.getCurrentHeight() - 75,
            delids: "", //删除id,
            // topTab: [ //tab分页
            //     {
            //       title: getLabel(15059, '默认共享'),
            //       viewcondition: "2"
            //     },
            //     {
            //       title: getLabel(18574,'非默认共享') ,
            //       viewcondition: "1"
            //     },
            // ],
            // selectedKey: "2", //当前tab页
            showTab:false,//是否展示分页
        };

        this.store = this.props.goalDetailStore;
    }

    componentWillMount() {
        //一些初始化请求
        const { isOrgGoal, setDisabled } = this.store;
        // let params = {}
        // if(isOrgGoal){ //有tab分页
        //     this.setState({showTab:true})
        //     params.workrelatetype = "2";
        // }else{
        //     this.setState({selectedKey:"1"})
        //     params.workrelatetype = "1"
        // }
        setDisabled(true);
        this.getShareSearch();
    }

    componentWillReceiveProps(){
        const { setDisabled } = this.store;
        setDisabled(true);
    }


    render() {
        let { hasRight, shareTableStore, shareConditioninfo, detailData, disabled } = this.store;
        let { showOperation, selectedRowKeys, rightMenu, delids, topTab, showTab} = this.state;
        return (
            <div className="wea-workrelate-share">
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@wte4zy`} datas={rightMenu}>
                    {detailData.canedit && <div className={showTab ? "share-title-div1" : "share-title-div"}>
                        <div className="share-title-div-button">
                            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@l8ix6e`} type="primary" className="share-button" title={getLabel(611, '添加')} onClick={this.addShare}><span className="icon-coms-Add-to-hot"></span></Button>
                            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@tzvuyi`} type="primary" className="share-button" disabled={disabled} title={getLabel(91, '删除')} onClick={this.del}><span className="icon-coms-form-delete-hot"></span></Button>
                        </div>
                    </div>}
                    {/* {
                        showTab && <WeaTab
                            datas={topTab}
                            keyParam="viewcondition" //主键
                            selectedKey={selectedKey}
                            onChange={v => this.onChangeTab(v)}
                        >
                        </WeaTab>
                    } */}
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@9jjtb1`} // table内部做了loading加载处理，页面就不需要再加了
                            comsWeaTableStore={shareTableStore} // table store
                            hasOrder={true} // 是否启用排序
                            needScroll={true} // 是否启用table内部列表滚动，将自适应到父级高度
                            rowSelection={{
                                selectedRowKeys: selectedRowKeys,
                                onChange: this.onSelectChange
                            }}
                            pagination={false}
                            onOperatesClick={this.onOperatesClick} //右键菜单操作
                            scroll={{y:window.innerHeight * 0.5}}
                        />
                    <WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@15pl6o`} />
                    <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@cux7s8`}
                        visible={showOperation}
                        conditions={toJS(shareConditioninfo)}
                        title={getLabel(611, '添加')}
                        onOk={(data) => {
                            this.operationShare(data);
                        }}
                        onCancel={() => {
                            this.setState({ showOperation: false });
                        }}
                    />
                </WeaRightMenu>
            </div>
        )
    }

    // //切换tab
    // onChangeTab = (v) =>{
    //     this.setState({selectedKey:v})
    //     this.getShareSearch(v);
    // }

    getShareSearch = (v) =>{
        const { setDisabled } = this.store;
        setDisabled(true);
        let params = {
            workrelatetype:"1"
        }

        this.store.getShareSearch(params);
    }

    //打开权限dialog
    addShare = () => {
        this.setState({ showOperation: true });
    }
    //新增
    operationShare = (data) => {
        let params = { ...data };
        params.operate = "add";
        params.workrelatetype = "1"
        params.bizid = this.store.detailKey
        params.hasRight = this.store.detailData.canedit
        // params.resourceid = this.store.resourceid
        params.doback = ()=>{
            this.getShareSearch();
        }
        this.store.shareOperate(params);
        this.setState({ showOperation: false });
    }
    //每行右侧操作数据
    onOperatesClick = (record, index, operate, flag) => {
        let id = record.id;
        if (id.length > 0) {
            this.delDate(id);
        }
    }
    //删除
    del = () => {
        let { delids } = this.state;
        this.delDate(delids);
    }
    delDate = (ids) => {
        // const { selectedKey } = this.state;
        if (ids.length > 0) {
            Modal.confirm({
                title: getLabel(386786, '信息确认'),
                content: getLabel(508867, '确定删除已选数据？'),
                onOk: () => {
                    this.store.shareOperate({ ids: ids, operate: "del", workrelatetype: "1", bizid: this.store.detailKey, hasRight: this.store.detailData.canedit,doback:()=>{this.getShareSearch()} });
                    this.setState({ selectedRowKeys: [], delids: [], rightMenu: this.updateStateArray(1, "disabled", true, this.state.rightMenu) });
                    if (typeof this.props.onCallBack === "function") {
                        this.props.onCallBack(false);
                    }
                },
                onCancel: () => {
                    return false;
                }
            })
        } else {
            message.error(getLabel(508946, '删除勾选不能为空'));
            return;
        }
    }
    //勾选check框
    onSelectChange = (selectedKeys, selectedRows) => {
        let { setDisabled } = this.store;
        let keys = toJS(selectedKeys);
        let len = keys.length;
        let { rightMenu } = this.state;
        let ids = "";
        for (let i = 0; i < len; i++) {
            ids += keys[i] + ",";
        }
        if (ids != "") {
            ids = ids.substring(0, ids.length - 1);
        }
        this.setState({ delids: ids });
        if (ids.length > 0) {
            if (typeof this.props.onCallBack === "function") {
                this.props.onCallBack(true);
            }
            setDisabled(false)
            this.setState({ rightMenu: this.updateStateArray(1, "disabled", false, rightMenu) });
        } else {
            if (typeof this.props.onCallBack === "function") {
                this.props.onCallBack(false);
            }
            setDisabled(true)
            this.setState({ rightMenu: this.updateStateArray(1, "disabled", true, rightMenu) });
        }
    };
    updateStateArray = (i, key, value, arr) => {
        arr.map(item => {
            if (item.key == i) {
                item[key] = value;
            }
        })
        return arr;
    }


}